package asapIs134CK.womi.client;

import asapIs134CK.womi.client.event.ListWomiEventDisplay;
import asapIs134CK.womi.client.games.GameOverHandler;
import asapIs134CK.womi.client.games.PickUpHandler;
import asapIs134CK.womi.client.games.PickupEvent;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitGameWigdet;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitPickerWidget;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitWidgetsFactory;
import asapIs134CK.womi.client.games.mathEnigme.ArithmeticSequence;
import asapIs134CK.womi.client.games.sudoku.Sudoku;
import asapIs134CK.womi.client.profile.UserDisplay;
import asapIs134CK.womi.client.profile.admintools.UserManagmentToolWidget;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class WeaponOfMathInstruction implements EntryPoint {
	
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";


	private Command froggitPickerLauncher = null;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Menu initialization 
		Panel divMenu = RootPanel.get("menu");
		MenuBar menu = new MenuBar();
		MenuBar puzzleSubMenu = new MenuBar();
		menu.addStyleName("Menu");
		Command cmd = new Command(){

			//@Override
			public void execute() {
				Panel divContent = RootPanel.get("content");
				divContent.clear();
				ListWomiEventDisplay wed = new ListWomiEventDisplay();
				divContent.add(wed);
			}
		};
		Command displaySudoku = new Command(){

			//@Override
			public void execute() {
				Panel divContent = RootPanel.get("content");
				divContent.clear();
				Sudoku sud = new Sudoku();
				divContent.add(sud);					 
			}
			
		};
		Command displayArithmetic = new Command(){

			//@Override
			public void execute() {
				Panel divContent = RootPanel.get("content");
				divContent.clear();
				ArithmeticSequence ar = new ArithmeticSequence();
				divContent.add(ar);	
				//ar.setFocus(true);
			}
			
		};
		
		froggitPickerLauncher = new Command() {
			
			@Override
			public void execute() {
				final Panel divContent = RootPanel.get("content");
				divContent.clear();
				final FroggitPickerWidget picker = new FroggitPickerWidget();
				picker.addPickupHandler(new PickUpHandler() {
					
					@Override
					public void onPickup(PickupEvent evt) {
						FroggitWidgetsFactory fact = (FroggitWidgetsFactory) evt.getPickedUp();
						divContent.remove(picker);
						FroggitGameWigdet frgGame = fact.createGameWidget();
						frgGame.addGameOverHandler(new GameOverHandler() {
							
							@Override
							public void onGameOver() {
								if(froggitPickerLauncher != null){
									froggitPickerLauncher.execute();
								}else{
									System.out.println("oups froggitPickerLancher == Null !!!!");
								}
							}
						});
						//TODO add call back on score submit for the server
						divContent.add(frgGame);
					}
				});
				divContent.add(picker);
			}
		};
		
		MenuItem home = new MenuItem("Home", true, cmd);
		home.addStyleName("MenuItem");
		MenuItem froggit = new MenuItem("Froggit", true, froggitPickerLauncher);
		MenuItem sudoku = new MenuItem("Sudoku", true, displaySudoku);
		MenuItem arithmetic = new MenuItem("Arithmetic", true, displayArithmetic);
		puzzleSubMenu.addItem(froggit);
		puzzleSubMenu.addItem(sudoku);
		puzzleSubMenu.addItem(arithmetic);
		MenuItem puzzles = new MenuItem("Puzzles", puzzleSubMenu);
		puzzles.addStyleName("MenuItem");
		
		Command launchUsrMgm= new Command() {
			
			@Override
			public void execute() {
				final Panel divContent = RootPanel.get("content");
				divContent.clear();
				UserManagmentToolWidget usrmgm = new UserManagmentToolWidget();
				divContent.add(usrmgm);
			}
		};
		MenuItem quizzes = new MenuItem("Quizzes", true, launchUsrMgm);
		quizzes.addStyleName("MenuItem");
		MenuItem results = new MenuItem("Results", true, cmd);
		results.addStyleName("MenuItem");
		menu.addItem(home);
		menu.addItem(puzzles);
		menu.addItem(quizzes);
		menu.addItem(results);
		
		divMenu.add(menu);
	
		// Left panel initialization 
		Panel divLeft = RootPanel.get("innerLeftColumn");
		UserDisplay userDisplay = UserDisplay.getInstance();
		divLeft.add(userDisplay);
		

		Panel divContent = RootPanel.get("content");
		divContent.clear();
		ListWomiEventDisplay wed = new ListWomiEventDisplay();
		divContent.add(wed);
	}
}
